package com.easy.service.impl;

import com.easy.bean.Maintenance_record;
import com.easy.mapper.MaintenanceRecordDao;
import com.easy.service.MaintenanceRecordService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaintenanceRecordServiceImpl implements MaintenanceRecordService {
    
    @Autowired
    private MaintenanceRecordDao maintenanceRecordDao;
    
    @Override
    public Result add(Maintenance_record maintenanceRecord) {
        // 设置默认值
        if (maintenanceRecord.getIs_deleted() == null) {
            maintenanceRecord.setIs_deleted(0);
        }
        
        int result = maintenanceRecordDao.add(maintenanceRecord);
        if (result > 0) {
            return Result.success("添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }
    
    @Override
    public Result del(Integer id) {
        int result = maintenanceRecordDao.del(id);
        if (result > 0) {
            return Result.success("删除成功");
        } else {
            return Result.fail(500, "删除失败");
        }
    }
    
    @Override
    public Result edit(Maintenance_record maintenanceRecord) {
        int result = maintenanceRecordDao.edit(maintenanceRecord);
        if (result > 0) {
            return Result.success("修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }
    
    @Override
    public Result one(Integer id) {
        Maintenance_record maintenanceRecord = maintenanceRecordDao.one(id);
        if (maintenanceRecord != null) {
            return Result.success(maintenanceRecord);
        } else {
            return Result.fail(500, "未找到指定记录");
        }
    }
    
    @Override
    public Result all(Integer equipment_id, Integer technician_id) {
        List<Maintenance_record> list = maintenanceRecordDao.all(equipment_id, technician_id);
        return Result.success(list);
    }
    
    @Override
    public Result page(Page page, Maintenance_record maintenanceRecord) {
        // 查询总数
        int totalCount = maintenanceRecordDao.pageCount(maintenanceRecord.getEquipment_id(), maintenanceRecord);
        page.setCount(totalCount);
        
        // 查询当前页数据
        List<Maintenance_record> list = maintenanceRecordDao.page(page, maintenanceRecord.getEquipment_id(), maintenanceRecord);
        page.setData(list);
        
        return Result.success(page);
    }
}